package com.hw.weathershow.util;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.datatype.jdk8.BaseScalarOptionalDeserializer;
import com.hw.weathershow.entity.Weather;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Cejay1314
 * @version 1.0
 * @create 2022/12/4 20:36
 */
public class PojoGenUtil {
    private static PojoGenUtil target;
    public PojoGenUtil(){
        target = null;
    }
    public static PojoGenUtil getInstance(){
        if(target==null){
            target = new PojoGenUtil();
        }
        return target;
    }
    public void hello(){
        System.out.println("hello");
    }
    public Weather getWeather(ResponseEntity<String> responseEntity){
        Map<String,Object> body = StringUtil.getInstance().stringTransformIntoMap(responseEntity.getBody());
        body = JSONObject.parseObject(responseEntity.getBody());
        List<String> arrayList = (List<String>)(body.get("lives"));
        String x = String.valueOf(arrayList.get(0));
        Map<String,String> headers = StringUtil.getInstance().stringTransformIntoMapString(x);
//        headers = JSONObject.parseObject(x);
        String city = headers.get("city");
        String province = headers.get("province");
        String weather= headers.get("weather");
        String temperature= headers.get("temperature");
        String windDirection= headers.get("winddirection");
        String windPower= headers.get("windpower");
        String humidity= headers.get("humidity");
        return new Weather(
                city,
                province,
                weather,
                temperature,
                windDirection,
                windPower,
                humidity
        );
    }
}
